package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.adapter.ShopAdapter;
import com.example.lenovo.a203_yhy_ordering_3.beans.ShopBean;
import com.example.lenovo.a203_yhy_ordering_3.iface.ShopIface;
import com.example.lenovo.a203_yhy_ordering_3.listener.ShopListener;
import com.example.lenovo.a203_yhy_ordering_3.service.ShopService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/11/21.
 */

public class ShopModel implements ShopIface{
    private Retrofit retrofit;

    public ShopModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getShopResult(final ShopListener shopListener) {
        ShopService shopListService = retrofit.create(ShopService.class);

        Call<List<ShopBean>> shopCall = shopListService.getAllShops();

        shopCall.enqueue(new Callback<List<ShopBean>>() {
            @Override
            public void onResponse(Call<List<ShopBean>> call, Response<List<ShopBean>> response) {
                if(response!=null)
                    shopListener.onResponse(response.body());
                else shopListener.onFail("获取商店列表失败");
            }

            @Override
            public void onFailure(Call<List<ShopBean>> call, Throwable t) {
                shopListener.onFail(t.toString());
            }
        });
    }
}
